#!/usr/bin/env python3
import csv
import shutil
from pathlib import Path

in_csv = Path('/home/ubantu00/zk_demo/build/results_summary.csv')
backup = in_csv.with_suffix('.csv.bak')

# make backup
shutil.copy2(in_csv, backup)

# read robustly using csv.reader then rewrite with csv.writer
rows = []
with in_csv.open(newline='') as f:
    reader = csv.reader(f)
    for r in reader:
        # flatten any accidental newlines inside cells by joining
        rows.append([c.replace('\n',' ').replace('\r',' ').strip() for c in r])

with in_csv.open('w', newline='') as f:
    writer = csv.writer(f)
    for r in rows:
        writer.writerow(r)

print('Backup written to', backup)
print('Cleaned', in_csv)
